/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package majordome.controller;

import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Valère
 */
public class Controller 
{
    private CalendarService calendarService;
    private ContactsService contactService;
    private String username;
    private String password;

    private Controller(String username, String password) throws AuthenticationException 
    {
        this.username = username;
        this.password = password;
        calendarService = new CalendarService("butler");
        contactService = new ContactsService("butler");
        calendarService.setUserCredentials(username, password);
        contactService.setUserCredentials(username, password);
    }
    
    /**
     * Retrun a controller
     * @param username
     * @param password
     * @return
     * @throws ServiceException 
     */
    public static Controller connect(String username,String password) throws ServiceException
    {
        return new Controller(username, password);
    }
    
    /**
     * Return all the calendars
     * @return 
     */
    public List<String> getCalendars()
    {
        try 
        {
            List<String> retour = new ArrayList<>();
            
            URL feedUrl = new URL("http://www.google.com/calendar/feeds/"+ this.username + "/allcalendars/full");
            CalendarFeed resultFeed = calendarService.getFeed(feedUrl, CalendarFeed.class);

            for (int i = 0; i < resultFeed.getEntries().size(); i++) 
            {
              CalendarEntry entry = resultFeed.getEntries().get(i);
              retour.add(entry.getTitle().getPlainText());
            }
            return retour;
        }
        catch (ServiceException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (MalformedURLException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * 
     * @param begin
     * @param end
     * @return 
     */
    public List<String> getEvents(DateTime begin,DateTime end)
    {
        try
        {
            List<String> retour = new ArrayList<>();
            
            URL feedUrl = new URL("https://www.google.com/calendar/feeds/"+this.username+"/private/full");

            CalendarQuery myQuery = new CalendarQuery(feedUrl);
            myQuery.setMinimumStartTime(begin);
            myQuery.setMaximumStartTime(end);

            // Send the request and receive the response:
            CalendarEventFeed resultFeed = this.calendarService.query(myQuery, CalendarEventFeed.class);
            
            for(int i = 0; i < resultFeed.getEntries().size();i++)
            {
                CalendarEventEntry entry = resultFeed.getEntries().get(i);
                retour.add(entry.getPlainTextContent());
            }
            
            return retour;
        } 
        catch (MalformedURLException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (ServiceException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    /**
     * Return a list of all mail address of the contacts
     * @return 
     */
    public List<String> getContacts() 
    {
        try 
        {
            List<String> retour = new ArrayList<>();

            URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/"+this.username+"/full");
            ContactFeed resultFeed = contactService.getFeed(feedUrl, ContactFeed.class);

            for (int i = 0; i < resultFeed.getEntries().size(); i++) 
            {
              ContactEntry entry = resultFeed.getEntries().get(i);
              if(entry.getEmailAddresses() != null && entry.getEmailAddresses().size() > 0)
              {
                  retour.add(entry.getEmailAddresses().get(0).getAddress());
              }
            }
            return retour;
        }
        catch (ServiceException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (MalformedURLException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex) 
        {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
